﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace StorageBox
{
    using WinPoint = System.Windows.Point;
    public partial class MainWindow : Window
    {
        enum ApplyMove
        {
            按下,
            移动中,
            空闲,
        }

        #region 应用的移动操作
        ApplyMove _IsApplyState = ApplyMove.空闲;
        DateTime _IsDownTime;
        Border curElement;
        public void ApplyItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _IsDownTime = DateTime.Now;

            if (_IsApplyState == ApplyMove.空闲)
            {
                ApplyMovePoint = new() { X = 0, Y = 0 };
                _IsApplyState = ApplyMove.按下;

                //按住一秒时间
                Task.Run(() =>
                {
                    while (_IsApplyState == ApplyMove.按下)
                    {
                        Task.Delay(200).Wait();
                        if (_IsDownTime.AddSeconds(0.8) < DateTime.Now)
                        {
                            _IsApplyState = ApplyMove.移动中;
                            //给当前元素加上效果
                            curElement = sender as Border;
                            //尝试套一个黑边
                            curElement.Dispatcher.Invoke(() => { SY_Library.Animate.Begin<ThicknessAnimation, Thickness>(curElement, Border.BorderThicknessProperty, new(0), new(3), 250); });
                            break;
                        }
                    }
                });
            }

        }
        public WinPoint ApplyMovePoint = new() { X = 0, Y = 0 };
        public void ApplyItem_MouseMove(object sender, MouseEventArgs e)
        {
            if (curElement == null)
                return;
            var element = curElement;
            switch (_IsApplyState)
            {
                case ApplyMove.按下:
                    break;
                case ApplyMove.移动中:
                    Canvas.SetZIndex(element, 999);
                    var cur = Mouse.GetPosition(MainWindowView);
                    if (ApplyMovePoint.X != 0 && ApplyMovePoint.Y != 0)
                    {
                        var margin = element.Margin;
                        element.Margin = new(margin.Left + (cur.X - ApplyMovePoint.X), margin.Top + (cur.Y - ApplyMovePoint.Y), 0, 0);
                    }
                    ApplyMovePoint = cur;

                    //我们需要计算时间,如果拖动在一个位置到达1s没有再移动,执行移动操作
                    _OldMoveTime = DateTime.Now;
                    if (!_IsMove)
                    {
                        _IsMove = true;
                        Task.Run(() =>
                        {
                            while (_IsMove)
                            {
                                Task.Delay(100).Wait();
                                if (_OldMoveTime.AddSeconds(0.8) < DateTime.Now)
                                {
                                    //计算当前鼠标位置[到此为止][先去写启动逻辑]
                                    var point = Mouse.GetPosition(this);

                                    Console.WriteLine($"X:{point.X},Y:{point.Y}");

                                    _IsMove = false;
                                }
                            }
                        });
                    }
                    break;
                case ApplyMove.空闲:
                    break;
                default:
                    break;
            }

        }
        DateTime _OldMoveTime;
        bool _IsMove = false;
        public void ApplyItem_MouseUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (_IsApplyState == ApplyMove.移动中 && curElement != null)
            {
                Canvas.SetZIndex(curElement, 0);
                ResetPoint(curElement);
            }
            _IsApplyState = ApplyMove.空闲;
            curElement = null;
        }
        public void ApplyItem_MouseLeave(object sender, MouseEventArgs e)
        {
            if (_IsApplyState == ApplyMove.移动中 && curElement != null)
            {
                var element = sender as Border;
                Canvas.SetZIndex(curElement, 0);
                ResetPoint(curElement);
            }
            _IsApplyState = ApplyMove.空闲;
            curElement = null;
        }

        private void ResetPoint(Border sp)
        {
            var key = Convert.ToInt32(sp.Name.Split('_')[1]);
            var element = ReaderManager.GetRender<DesktopReader>().GetApplyByKey(key);
            //复位
            var left = element.Location.X * (ApplyStaticData.Marage + ApplyStaticData.MinSideLength.Width) + ApplyStaticData.Marage;
            var top = element.Location.Y * (ApplyStaticData.Marage + ApplyStaticData.MinSideLength.Height) + ApplyStaticData.Marage;
            //SY_Library.Animate.Move(sp, sp.Margin, new(left, top, 0, 0), 250);//此处动画可以进行改动
            sp.Margin = new(left, top, 0, 0);//被加上动画后没法直接赋值移动[可能需要继承Broder][暂时写成直接赋值]
            SY_Library.Animate.Begin<ThicknessAnimation, Thickness>(sp, Border.BorderThicknessProperty, new(3), new(0), 300);
        }
        #endregion

        private void StorageBoxCanvas_Loaded(object sender, RoutedEventArgs e)
        {
            ReaderManager.Show<DesktopReader>();
        }

        public void DefaultApplyAdd_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
        public void DefaultApplyAdd_MouseUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            #region 创建元素
            //Random r = new Random();
            //var data = new ApplyData()
            //{
            //    Langth = new Size() { Width = r.Next(1, 5), Height = r.Next(1, 4) }
            //};

            //BoxApplication.GetManager<ReaderManager>().GetRender<DesktopReader>().ShowItem(data);
            //StackPanel sp = new() { Background = SY_Library.Color.BackColor(255, 0, 0, 100), Width = 100, Height = 100 };
            #endregion
            #region 拉起提示框
            //var element = ElementFactoryManager.Create<AddApplyElement, AddApplyData>(AddApplyFactory.GetData());

            //BoxApplication.GetManager<ReaderManager>().Show<ShowMessageReader>(u =>
            //{
            //    u.SetChildElement(element.RootElement);
            //});
            #endregion
            #region 弹出选择菜单

            #endregion
            //此处我需要一个组合元素
        }

    }
}
